/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.woundedwolf.android.tumbleweed.renderables;

import com.woundedwolf.android.tumbleweed.Polygon;
import com.woundedwolf.android.tumbleweed.elements.Renderable;
import com.woundedwolf.android.tumbleweed.elements.Vector2D;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import javax.microedition.khronos.opengles.GL10;

/**
 *
 * @author zagnut
 */
public class Polygons implements Renderable
{

        private final Object polygonLock;
        private ArrayList<Polygon> polygonList;

        public Polygons()
        {
                polygonLock = new Object();
                polygonList = new ArrayList<Polygon>();
        }

        public void addPolygon(Polygon poly)
        {
                synchronized (polygonLock)
                {
                        polygonList.add(poly);
                }
        }

        public ArrayList<Polygon> getPolygons()
        {
                return polygonList;
        }

        public void removePolygon(Polygon poly)
        {
                synchronized (polygonLock)
                {
                        polygonList.remove(poly);
                }
        }

        public void draw(GL10 glContext)
        {
                synchronized (polygonLock)
                {
                        if (polygonList.isEmpty())
                        {
                                return;
                        }

                        // Calculate size
                        int size = 0;
                        for (Polygon poly : polygonList)
                        {
                                size += poly.synchronizedPointsList.size();
                        }

                        float[] vertices = new float[size * 2];
                        float[] colors = new float[size * 4];

                        int i = 0;
                        for (Polygon poly : polygonList)
                        {
                                int color = poly.getColor();
                                for (Vector2D point : poly.synchronizedPointsList)
                                {
                                        vertices[i * 2] = (int) point.getXpos() + 0.5f;
                                        vertices[i * 2 + 1] = (int) point.getYpos() + 0.5f;
                                        colors[i * 4] = (byte) (color >> 16);
                                        colors[i * 4 + 1] = (byte) (color >> 8);
                                        colors[i * 4 + 2] = (byte) (color);
                                        colors[i * 4 + 3] = (byte) (color >> 24);
                                        i++;
                                }



                        }

                        // Allocate vertexbuffer.
                        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
                        vbb.order(ByteOrder.nativeOrder());
                        FloatBuffer vertexBuffer = vbb.asFloatBuffer();
                        vertexBuffer.put(vertices);
                        vertexBuffer.position(0);

                        ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);
                        cbb.order(ByteOrder.nativeOrder());
                        FloatBuffer colorBuffer = cbb.asFloatBuffer();
                        colorBuffer.put(colors);
                        colorBuffer.position(0);

                        glContext.glEnableClientState(GL10.GL_VERTEX_ARRAY);
                        glContext.glEnableClientState(GL10.GL_COLOR_ARRAY);
                        glContext.glVertexPointer(2, GL10.GL_FLOAT, 0, vertexBuffer);
                        glContext.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);

                        // Draw the polygons to screen
                        size = 0;
                        for (Polygon poly : polygonList)
                        {
                                glContext.glDrawArrays(GL10.GL_LINE_STRIP, size, poly.getPoints().size());
                                size += poly.getPoints().size();
                        }
                        
                        glContext.glDisableClientState(GL10.GL_COLOR_ARRAY);
                        glContext.glDisableClientState(GL10.GL_VERTEX_ARRAY);
                }
        }
}
